import { Exclude } from 'class-transformer';
import { Users } from '../../users/entities/user.entity';
import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  ManyToOne,
  ManyToMany,
  JoinTable,
} from 'typeorm';
import { PostInfoDto } from '../dto/find-post.dto';
import { ApiProperty } from '@nestjs/swagger';
import { Tags } from 'src/modules/tags/entities/tag.entity';
import { Category } from 'src/modules/categories/entities/category.entity';
export enum StatusContent {
  Publish = 'publish',
  Draft = 'draft',
}
@Entity('posts')
export class Posts {
  @ApiProperty({ description: '文章id' })
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @ApiProperty({ description: '文章标题' })
  @Column({
    comment: '文章标题',
    length: 50,
    nullable: false,
    default: null,
  })
  title: string;

  @ApiProperty({ description: 'markdown内容' })
  @Column({
    comment: 'markdown内容',
    type: 'mediumtext',
    default: null,
  })
  content: string;

  @ApiProperty({ description: 'markdown 转 html,自动生成' })
  @Column({
    comment: 'markdown 转 html,自动生成',
    type: 'mediumtext',
    default: null,
  })
  contentHtml: string;

  @ApiProperty({ description: '摘要，自动生成' })
  @Column({ comment: '摘要，自动生成', type: 'text', default: null })
  summary: string;

  @ApiProperty({ description: '封面图' })
  @Column({ comment: '封面图', default: null })
  coverUrl: string;

  @ApiProperty({ description: '阅读量' })
  @Column({ comment: '阅读量', type: 'int', default: 0 })
  count: number;

  @ApiProperty({ description: '点赞量' })
  @Column({ comment: '点赞量', type: 'int', default: 0 })
  likeCount: number;

  @ApiProperty({ description: '推荐显示' })
  @Column({
    comment: '推荐显示',
    type: 'tinyint',
    width: 1,
    default: false,
  })
  isRecommend: boolean;

  @ApiProperty({ description: '文章状态' })
  @Column('simple-enum', {
    comment: '文章状态',
    enum: ['draft', 'publish'],
    default: 'draft',
  })
  status: string;

  @ApiProperty({ description: '作者' })
  @ManyToOne(() => Users, (user) => user.posts)
  author: Users;

  @ApiProperty({ description: '分类' })
  @Exclude()
  @ManyToOne(() => Category, (category) => category.posts)
  category: Category;

  @ApiProperty({ description: '评论开启' })
  @Column({
    comment: '评论开启 true - 开启 , false - 禁用',
    default: true,
  })
  isCommentOpen: boolean;

  @ApiProperty({ description: '是否置顶' })
  @Column({
    comment: '是否置顶 true - 置顶 , false - 不置顶',
    default: false,
  })
  isTop: boolean;

  @ApiProperty({ description: '标签' })
  @ManyToMany(() => Tags, (tag) => tag.posts)
  @JoinTable()
  tags: Tags[];

  @ApiProperty({ description: '出版时间' })
  @Column({
    type: 'timestamp',
    comment: '出版时间',
    default: null,
  })
  publishTime: Date;

  @ApiProperty({ description: '创建时间' })
  @Column({
    type: 'timestamp',
    comment: '创建时间',
    default: () => 'CURRENT_TIMESTAMP',
  })
  createTime: Date;

  @ApiProperty({ description: '修改时间' })
  @Column({
    type: 'timestamp',
    comment: '修改时间',
    default: () => 'CURRENT_TIMESTAMP',
  })
  updateTime: Date;

  toResponseObject(): PostInfoDto {
    const responseObj: PostInfoDto = {
      ...this,
      isRecommend: this.isRecommend ? true : false,
      author: null,
      category: null,
      tags: [],
    };
    if (this.category) {
      responseObj.category = { name: this.category.name, id: this.category.id };
    }
    if (this.tags && this.tags.length) {
      responseObj.tags = this.tags.map((item) => {
        return {
          id: item.id,
          name: item.name,
        };
      });
    }
    if (this.author && this.author.id) {
      responseObj.author = {
        id: this.author.id,
        nickname: this.author.nickname || this.author.account,
      };
    }
    return responseObj;
  }
}
